package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.HotProductRecommendations;
import com.example.mall.dto.HotProductRecommendationsDto;
import com.example.mall.mapper.HotProductRecommendationsMapper;
import com.example.mall.query.HotProductRecommendationsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IHotProductRecommendationsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 热门商品推荐表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class HotProductRecommendationsServiceImpl extends ServiceImpl<HotProductRecommendationsMapper, HotProductRecommendations> implements IHotProductRecommendationsService {

    @Autowired
    private HotProductRecommendationsMapper hotProductRecommendationsMapper;

    @Override
    public PageDTO<HotProductRecommendationsDto> queryPage(HotProductRecommendationsQuery hotProductRecommendationsQuery) {
        if (hotProductRecommendationsQuery == null) {
            hotProductRecommendationsQuery = new HotProductRecommendationsQuery();
        }
        Page<HotProductRecommendations> page = hotProductRecommendationsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<HotProductRecommendations> cartPage = lambdaQuery()
                .like(hotProductRecommendationsQuery.getRecommendId()!= null, HotProductRecommendations::getRecommendId, hotProductRecommendationsQuery.getRecommendId())
                .like(hotProductRecommendationsQuery.getProductId()!= null, HotProductRecommendations::getProductId, hotProductRecommendationsQuery.getProductId())
                .like(hotProductRecommendationsQuery.getStatus()!= null, HotProductRecommendations::getStatus, hotProductRecommendationsQuery.getStatus())
                .orderByDesc(HotProductRecommendations::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, HotProductRecommendationsDto.class);
    }

    @Override
    public HotProductRecommendations queryHotProductRecommendationsById(Long recommendId) {
        AssertUtil.isNotNull(recommendId, "推荐ID不能为空");
        return hotProductRecommendationsMapper.selectById(recommendId);
    }

    @Override
    public List<HotProductRecommendations> queryHotProductRecommendationsList() {
        return this.list();
    }

    @Override
    public void insert(HotProductRecommendationsDto hotProductRecommendationsDto) {
        HotProductRecommendations hotProductRecommendations = new HotProductRecommendations();
        CopyUtils.copyProperties(hotProductRecommendationsDto, hotProductRecommendations);
        hotProductRecommendations.setRecommendId(CodeGenerateUtils.generateUniqueId());
        hotProductRecommendations.setCreateDate(new Date());
        hotProductRecommendations.setUpdateDate(new Date());
        hotProductRecommendations.setStatus(BaseConstants.EFFECTIVE_STATUS);
        hotProductRecommendationsMapper.insert(hotProductRecommendations);
    }

    @Override
    public void update(HotProductRecommendationsDto hotProductRecommendationsDto) {
        AssertUtil.isNotNull(hotProductRecommendationsDto.getRecommendId(), "推荐ID不能为空");
        HotProductRecommendations hotProductRecommendations = hotProductRecommendationsMapper.selectById(hotProductRecommendationsDto.getRecommendId());
        AssertUtil.isNotNull(hotProductRecommendations, "推荐不存在");
        CopyUtils.copyProperties(hotProductRecommendationsDto, hotProductRecommendations);
        hotProductRecommendations.setUpdateDate(new Date());
        hotProductRecommendationsMapper.updateById(hotProductRecommendations);
    }

    @Override
    public void delete(Long recommendId) {
        AssertUtil.isNotNull(recommendId, "推荐ID不能为空");
        HotProductRecommendations hotProductRecommendations = hotProductRecommendationsMapper.selectById(recommendId);
        AssertUtil.isNotNull(hotProductRecommendations, "推荐不存在");
        hotProductRecommendations.setStatus(BaseConstants.LOSE_STATUS);
        hotProductRecommendations.setUpdateDate(new Date());
        hotProductRecommendationsMapper.updateById(hotProductRecommendations);
    }

    @Override
    public void deleteBatch(List<Long> recommendIds) {
        AssertUtil.isNotNull(recommendIds, "推荐ID列表不能为空");
        List<HotProductRecommendations> hotProductRecommendations = hotProductRecommendationsMapper.selectBatchIds(recommendIds);
        AssertUtil.isNotNull(hotProductRecommendations, "推荐不存在");
        for (HotProductRecommendations hotProductRecommendation : hotProductRecommendations) {
            hotProductRecommendation.setStatus(BaseConstants.LOSE_STATUS);
            hotProductRecommendation.setUpdateDate(new Date());
            hotProductRecommendationsMapper.updateById(hotProductRecommendation);
        }
    }
}
